﻿package mortal.game.control
{
    import Message.Game.*;
    import Message.Public.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.view.CollectExchange.panel.*;
    import mortal.game.view.DoubleElevenCollectExchange.*;
    import mortal.game.view.LineOffReward.*;
    import mortal.game.view.levelReward.*;
    import mortal.game.view.levelReward.panel.*;
    import mortal.game.view.signUp.*;
    import mortal.game.view.signUp.data.*;
    import mortal.game.view.signUp.lineOn.*;
    import mortal.game.view.signUp.sevenDayLogin.*;
    import mortal.game.view.signUp.util.*;
    import mortal.game.view.signUp.view.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class SignUpController extends Controller
    {
        private var _signUpModule:SignUpModule;
        private var _lineOffController:LineOffRewardController;
        public var lineOffFirstReqData:Boolean = true;
        private var _lineOnController:LineOnRewardController;
        public var _sevenDayController:SevenDayLoginController;
        private var isHasGetCollectInfo:Boolean = false;

        public function SignUpController()
        {
            this._lineOffController = new LineOffRewardController();
            this._lineOnController = new LineOnRewardController();
            this._sevenDayController = new SevenDayLoginController();
            return;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.VipLevelUp, this.onVipUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.SignUpGetAllSignUpInfo, this.getAllSignUpdataHandler);
            NetDispatcher.addCmdListener(ServerCommand.LevelRewardHasGotGiftInfo, this.onLevelGiftInfoUpdate);
            NetDispatcher.addCmdListener(ServerCommand.CollectExchangeInfoGet, this.onCollectInfoGet);
            Dispatcher.addEventListener(EventName.SignUpOpenWinAndSwitchTag, this.onOpenWinSwitchTag);
            Cache.instance.role.roleEntityInfo.addEventListener(EntityInfoEventName.UpdateLevel, this.levelUpgrade);
            Dispatcher.addEventListener(EventName.SignUpCanGetReward, this.hasWelfareOrNot);
            Dispatcher.addEventListener(EventName.CollectExchangeGetOutput, this.onCollectGetOutput);
            Dispatcher.addEventListener(EventName.CollectExchangeInfoUpdate, this.onCollectInfoUpdate);
            Dispatcher.addEventListener(EventName.CollectExchangeTagUpdate, this.onCollectTagUpdate);
            NetDispatcher.addCmdListener(ServerCommand.NewDayUpdate, this.onDateChange);
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._signUpModule == null)
            {
                this._signUpModule = new SignUpModule();
                this._signUpModule.addEventListener(WindowEvent.SHOW, this.onWindowShow);
                this._signUpModule.addEventListener(WindowEvent.CLOSE, this.onWindowClose);
                this._lineOffController.setView(this._signUpModule);
                this._lineOnController.setView(this._signUpModule);
            }
            return this._signUpModule;
        }// end function

        private function onWindowShow(event:Event) : void
        {
            this._lineOffController.onShow(event);
            this._lineOnController.onShow(event);
            this._sevenDayController.onShow(event);
            EventUtil.addSelfEvent(EventName.SignUpCanGetReward, this.checkLineOnBtnShine, true);
            Dispatcher.dispatchEvent(new Event(EventName.CollectExchangeTagUpdate));
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.packUpdateHandler);
            return;
        }// end function

        private function onWindowClose(event:Event) : void
        {
            this._lineOffController.onClose(event);
            this._lineOnController.onClose(event);
            this._sevenDayController.onClose(event);
            EventUtil.addSelfEvent(EventName.SignUpCanGetReward, this.checkLineOnBtnShine, false);
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.packUpdateHandler);
            return;
        }// end function

        private function onVipUpdateHandler(param1:Object) : void
        {
            GameProxy.checkIn.getCheckInInfo();
            return;
        }// end function

        private function getAllSignUpdataHandler(param1:Object) : void
        {
            var _loc_4:Array = null;
            var _loc_5:Array = null;
            var _loc_2:* = param1.data as SCheckInInfo;
            var _loc_3:* = param1.state as String;
            if (_loc_2)
            {
                _loc_4 = _loc_2.checkInList;
                _loc_5 = _loc_2.checkInAwardTaken;
                cache.signUp.totalSignUpNum = _loc_4.length;
                cache.signUp.hasResignNum = _loc_2.checkInNowPostCheck;
                cache.signUp.saveSignUpdata(DataConvertUtil.convert(_loc_4));
                cache.signUp.hasReceiveRewards = _loc_5;
                cache.signUp.updateRewardTakeState();
                if (this._signUpModule && this._signUpModule.currSelView && this._signUpModule.currSelView is DailySignUpView && (this._signUpModule.currSelView as DailySignUpView).isCreateChildren)
                {
                    (this._signUpModule.currSelView as DailySignUpView).updateAll();
                }
                this.updateSignUpRemindInfo();
                if (_loc_3 == "getReward" && !cache.signUp.hasRewardToGet())
                {
                    this._signUpModule.hide();
                }
            }
            return;
        }// end function

        private function updateSignUpRemindInfo() : void
        {
            var _loc_1:Boolean = false;
            GameController.mapNavBar.rightTopBar.blinkBtn("signUp", SignUpUtil.isSignUpAvailable());
            if (SignUpUtil.getFirstCanTake() || SignUpUtil.isSignUpAvailable())
            {
                _loc_1 = true;
            }
            else
            {
                _loc_1 = false;
            }
            var _loc_2:Array = [SignUpTabType.DailySignUp, _loc_1];
            var _loc_3:* = Cache.instance.signUp.updateSignUpHasReward(_loc_2);
            this.notifyWelfareIcon(_loc_3);
            if (this._signUpModule)
            {
                this._signUpModule.updateAllTabShine();
            }
            return;
        }// end function

        private function onLevelGiftInfoUpdate(param1:SLevelGiftInfo) : void
        {
            Cache.instance.levelReward.hasGotLevelGiftInfo = param1;
            if (this._signUpModule && this._signUpModule.currSelView && this._signUpModule.currSelView is LevelRewardVipUpGiftPanel)
            {
                (this._signUpModule.currSelView as LevelRewardVipUpGiftPanel).refresh();
            }
            var _loc_2:* = Cache.instance.levelReward.canReceiveGiftsNum;
            var _loc_3:* = _loc_2 > 0 ? (true) : (false);
            var _loc_4:Array = [SignUpTabType.LevelRewardVipUpGift, _loc_3];
            var _loc_5:* = Cache.instance.signUp.updateSignUpHasReward(_loc_4);
            this.notifyWelfareIcon(_loc_5);
            if (this._signUpModule)
            {
                this._signUpModule.updateAllTabShine();
            }
            return;
        }// end function

        private function onCollectInfoGet(param1:SCollectSysInfo) : void
        {
            Cache.instance.collectReward.exchangeInfo = param1;
            if (this._signUpModule && this._signUpModule.currSelView)
            {
                if (this._signUpModule.currSelView is CollectExchangePanel)
                {
                    (this._signUpModule.currSelView as CollectExchangePanel).refresh();
                }
                else if (this._signUpModule.currSelView is DoubleElevenCollectExchangePanel)
                {
                    (this._signUpModule.currSelView as DoubleElevenCollectExchangePanel).refresh();
                }
            }
            this.onCollectUpdateTagShine();
            return;
        }// end function

        private function onCollectInfoUpdate(event:DataEvent) : void
        {
            var _loc_2:* = event.data["id"];
            var _loc_3:* = event.data["exchangeNum"];
            Cache.instance.collectReward.updateExchangeInfo(_loc_2, _loc_3);
            if (this._signUpModule && this._signUpModule.currSelView)
            {
                if (this._signUpModule.currSelView is CollectExchangePanel)
                {
                    (this._signUpModule.currSelView as CollectExchangePanel).refresh();
                }
                else if (this._signUpModule.currSelView is DoubleElevenCollectExchangePanel)
                {
                    (this._signUpModule.currSelView as DoubleElevenCollectExchangePanel).refresh();
                }
            }
            this.onCollectUpdateTagShine();
            return;
        }// end function

        private function onCollectTagUpdate(event:Event) : void
        {
            this.onCollectUpdateTagShine();
            return;
        }// end function

        private function onCollectGetOutput(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            GameProxy.collectExchange.getExchangeReward(_loc_2);
            return;
        }// end function

        private function onCollectShowUpdate() : void
        {
            if (this._signUpModule && this._signUpModule.currSelView)
            {
                if (this._signUpModule.currSelView is CollectExchangePanel)
                {
                    (this._signUpModule.currSelView as CollectExchangePanel).refresh();
                }
                else if (this._signUpModule.currSelView is DoubleElevenCollectExchangePanel)
                {
                    (this._signUpModule.currSelView as DoubleElevenCollectExchangePanel).refresh();
                }
            }
            this.onCollectUpdateTagShine();
            return;
        }// end function

        private function onCollectUpdateTagShine() : void
        {
            var _loc_1:int = 0;
            var _loc_2:Boolean = false;
            var _loc_3:Array = null;
            var _loc_4:Boolean = false;
            Cache.instance.signUp.SetTagNameDic();
            if (Cache.instance.signUp.getTagOpenStateByIndex(SignUpConst.Tag_Collect_Exchange) == 0)
            {
                _loc_1 = Cache.instance.collectReward.canNormalExchangeNum;
                _loc_2 = _loc_1 > 0 ? (true) : (false);
                _loc_3 = [SignUpTabType.CollectExchange, _loc_2];
                _loc_4 = Cache.instance.signUp.updateSignUpHasReward(_loc_3);
                this.notifyWelfareIcon(_loc_4);
                if (this._signUpModule)
                {
                    this._signUpModule.updateAllTabShine();
                }
            }
            if (Cache.instance.signUp.getTagOpenStateByIndex(SignUpConst.Tag_DoubleEleven_Collect) == 0)
            {
                _loc_1 = Cache.instance.collectReward.canDoubleElevenExchangeNum;
                _loc_2 = _loc_1 > 0 ? (true) : (false);
                _loc_3 = [SignUpTabType.DoubleElevenCollectExchange, _loc_2];
                _loc_4 = Cache.instance.signUp.updateSignUpHasReward(_loc_3);
                this.notifyWelfareIcon(_loc_4);
                if (this._signUpModule)
                {
                    this._signUpModule.updateAllTabShine();
                }
            }
            return;
        }// end function

        private function packUpdateHandler(event:Event) : void
        {
            this.onCollectShowUpdate();
            return;
        }// end function

        private function notifyWelfareIcon(param1:Boolean) : void
        {
            if (param1)
            {
                WelfareIcon.instance.playEffect();
            }
            else
            {
                WelfareIcon.instance.stopEffect();
            }
            return;
        }// end function

        private function levelUpgrade(event:Event) : void
        {
            if (this._signUpModule)
            {
                this._signUpModule.checkTagIsSatisfy();
                if (this._signUpModule.currSelView && this._signUpModule.currSelView is LevelRewardVipUpGiftPanel)
                {
                    (this._signUpModule.currSelView as LevelRewardVipUpGiftPanel).refresh();
                }
            }
            var _loc_2:* = Cache.instance.levelReward.canReceiveGiftsNum;
            var _loc_3:* = _loc_2 > 0 ? (true) : (false);
            var _loc_4:Array = [SignUpTabType.LevelRewardVipUpGift, _loc_3];
            var _loc_5:* = Cache.instance.signUp.updateSignUpHasReward(_loc_4);
            this.notifyWelfareIcon(_loc_5);
            if (this._signUpModule)
            {
                this._signUpModule.updateAllTabShine();
            }
            this.updateMainBtnGiftBtnShine();
            return;
        }// end function

        private function updateMainBtnGiftBtnShine() : void
        {
            var _loc_1:* = Cache.instance.signUp.hasRewardToGet();
            this.notifyWelfareIcon(_loc_1);
            return;
        }// end function

        private function onOpenWinSwitchTag(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            (view as SignUpModule).openAndSelTag(_loc_2);
            return;
        }// end function

        private function checkLineOnBtnShine(param1:Object) : void
        {
            if (this._signUpModule)
            {
                this._signUpModule.updateAllTabShine();
            }
            return;
        }// end function

        private function onActivationTakeBack(param1:Object) : void
        {
            var _loc_2:* = param1 as int;
            if (_loc_2 > 0)
            {
                MsgManager.showMouseMsg(Language.getString(20433), MsgRollTipsType.msgRollTips1);
            }
            else
            {
                switch(_loc_2)
                {
                    case SignUpConst.State_Active_HasTaken:
                    {
                        MsgManager.showMouseMsg(Language.getString(20434), MsgRollTipsType.msgRollTips3);
                        break;
                    }
                    case SignUpConst.State_Active_NotExist:
                    {
                        MsgManager.showMouseMsg(Language.getString(20435), MsgRollTipsType.msgRollTips3);
                        break;
                    }
                    default:
                    {
                        MsgManager.showMouseMsg(Language.getString(20436), MsgRollTipsType.msgRollTips3);
                        break;
                        break;
                    }
                }
            }
            return;
        }// end function

        private function hasWelfareOrNot(event:DataEvent) : void
        {
            var _loc_3:Array = null;
            var _loc_4:Boolean = false;
            var _loc_2:* = event.data as Array;
            if (_loc_2)
            {
                _loc_3 = [_loc_2[1], _loc_2[2]];
                _loc_4 = Cache.instance.signUp.updateSignUpHasReward(_loc_3);
                this.notifyWelfareIcon(_loc_4);
            }
            return;
        }// end function

        private function onDateChange(param1:Object) : void
        {
            GameProxy.checkIn.getCheckInInfo();
            return;
        }// end function

    }
}
